import { Tool } from '../../../Tool';

/**
 * 隐藏工具
 */
export class HiddenTool extends Tool {
	constructor(viewer) {
		super(arguments);
		this.viewer = viewer;
		this.hideElements = {};
		this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	}

	start() {
		this.handler.setInputAction((e) => {
			const clickObj = this.viewer.scene.pick(e.position);
			if (Cesium.defined(clickObj) && typeof clickObj.id === 'string') {
				const layer = clickObj.primitive;
				const name = layer.name;
				layer.setOnlyObjsVisible([clickObj.id], false);
				if (!this.hideElements[name]) {
					this.hideElements[name] = [clickObj.id];
				} else {
					this.hideElements[name].push(clickObj.id);
				}
			}
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
	}

	cancel() {
		this.handler = this.handler && this.handler.destroy();
		const names = Object.keys(this.hideElements);
		for (const name of names) {
			const layer = this.viewer.scene.layers.find(name);
			layer.setOnlyObjsVisible(this.hideElements[name], true);
		}
	}
}
HiddenTool.toolId = 'HiddenTool';
